import 'package:flutter/material.dart';
import 'package:flutter_main_demo/widgets/keep_alive_wrapper.dart';
import 'package:get/get.dart';

import 'logic.dart';

class HomePage extends StatelessWidget {
  HomePage({Key? key}) : super(key: key);

  final logic = Get.find<HomeLogic>();
  final state = Get.find<HomeLogic>().state;

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: state.tabs.length,
      child: Scaffold(
        backgroundColor: const Color(0xFFE3E6EE),
        appBar: AppBar(
          backgroundColor: const Color(0xFFE3E6EE),
          title: _buildTabBar(),
        ),
        body: TabBarView(
          physics: const NeverScrollableScrollPhysics(),
          children: [
            KeepAliveWrapper(child: Placeholder()),
            KeepAliveWrapper(child: Placeholder()),
          ],
        ),
      ),
    );
  }

  Widget _buildTabBar() {
    return TabBar(
      isScrollable: true,
      indicatorSize: TabBarIndicatorSize.label,
      indicatorColor: Colors.black,
      labelPadding: const EdgeInsets.only(right: 20),
      unselectedLabelColor: const Color(0xFF999999),
      labelColor: Colors.black87,
      labelStyle: const TextStyle(fontSize: 16, color: Colors.black87),
      unselectedLabelStyle: const TextStyle(fontSize: 16),
      overlayColor: const MaterialStatePropertyAll<Color>(Colors.transparent),
      indicatorWeight: 3.0,
      onTap: (index) => logic.setTabIndex(index),
      tabs: state.tabs.map((item) => Tab(text: item)).toList(),
    );
  }
}
